package ltd.dujiabao.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.msmservice.service.MsmService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author : DuJiabao
 * @Project : online-course-project
 * @Time : 2021/4/24 14:45
 * @Desc :
 */
@Service
@Slf4j
public class MsmServiceImpl implements MsmService {
  @Override
  public boolean send(String phone, String templateCode, Map<String, Object> param) {
    DefaultProfile profile =
            DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF");
    IAcsClient client = new DefaultAcsClient(profile);

    CommonRequest request = new CommonRequest();
    //request.setProtocol(ProtocolType.HTTPS);
    request.setMethod(MethodType.POST);
    request.setDomain("dysmsapi.aliyuncs.com");
    request.setVersion("2017-05-25");
    request.setAction("SendSms");

    request.putQueryParameter("PhoneNumbers", phone);
    request.putQueryParameter("SignName", "我的谷粒在线教育网站");
    request.putQueryParameter("TemplateCode", templateCode);
    request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

    try {
      CommonResponse response = client.getCommonResponse(request);
      log.info("返回的数据：" + response.getData());
      return response.getHttpResponse().isSuccess();
    } catch (ClientException e) {
      e.printStackTrace();
    }
    return false;
  }
}
